export default {
	config: {
        pre:'',//前缀
		key:null,
		data:null,
		type:'storage',
		df:null,//默认值
		time:2592000,//缓存时间
		clear:false,
		success:null
	},
    interceptor:null,
    init(_conf={},interceptor=null){
        this.config=Object.assign(this.config,_conf)
        if(interceptor){
            this.interceptor=interceptor
        } 
    },
	get(key,df=''){
        return this.S({
            key,
            df
        })
    },
    clear(key){
        return this.S({
            key,
            clear:true 
        })
    },
    remove(key){
      this.clear(key) 
    },
    set(key,data,time=0){
        if(time==0){
            time=this.config.time
        }
        return this.S({
            key,
            data,
            time
        })
    },
	S(options)
	{
		let _config=Object.assign({}, this.config, options) 
		if(_config.key!=null)
		{
            _config.key=_config.pre+_config.key
			let realkey=_config.key
			if(this.interceptor)
			{
				this.interceptor(_config)
				if(_config.key==null)
				{
					return _config.df
				}
			}
			//清除缓存
			if(_config.clear)
			{
				if (process.env.NODE_ENV === 'development')
				{
					console.log('---remove cache----key='+realkey);
				}
				try{
					uni.removeStorage({
						key:_config.key
					})

				}catch(e){
					//TODO handle the exception
				}
			}
			//添加缓存
			else if(_config.data!=null){
				let timestamp=parseInt(new Date().getTime()/1000);
				let StorageData={
					data:_config.data,
					time:_config.time+timestamp
				};
				uni.setStorage({
					key:_config.key,
					data:StorageData,
					success(r){
						//console.log(r)
					},
					fail(err) {
					//	console.log(r)
					}

				});
				if (process.env.NODE_ENV === 'development')
				{
					console.log('---add cache----key='+realkey+',time='+(_config.time+timestamp));
				}
				_config.df=_config.data;
			}
			else
			{
				//取出缓存
				try {
					if(_config.success){
						uni.getStorage({
							key:_config.key,
							success(rs) {
							_config.success(rs.data)
							},
							fail() {
								_config.success(null)
							}

						})
						return _config.df
					}

					const value = uni.getStorageSync(_config.key);


					if (value) {
						let timestamp=parseInt(new Date().getTime()/1000);
						if(value.data&&value.time&&value.time>timestamp)
						{
							if (process.env.NODE_ENV === 'development')
							{
								console.log('---from cache----key='+realkey+',time='+(value.time-timestamp));
							}
							_config.df=value.data;
						}
						else
						{
							if (process.env.NODE_ENV === 'development')
							{
								console.log('---cache gq----key='+realkey);
								//options.df=null;
							}
						}
					}
					//return options.df;
				} catch (e) {
					// error
					//return options.df;
				}
			}
		}
		else
		{
			if (process.env.NODE_ENV === 'development')
			{
				console.log('the key cannot empty!');
			}
		}
		return _config.df;
	}
}
